package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.StatementData;
import com.xmy.cultivate.exception.CommonException;
import com.xmy.cultivate.mapper.GradeMapper;
import com.xmy.cultivate.mapper.OrderDetailMapper;
import com.xmy.cultivate.mapper.QuarterMapper;
import com.xmy.cultivate.mapper.StudentMapper;
import com.xmy.cultivate.service.IFinanceService;
import com.xmy.cultivate.util.ResultCode;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class FinanceServiceImpl implements IFinanceService {

    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    GradeMapper gradeMapper;

    @Autowired
    QuarterMapper quarterMapper;

    @Override
    public IPage<StatementData> statementData(IPage<?> page,String schoolId, String dateTime, String subjectsId) {


        QueryWrapper<Quarter> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("start_date",dateTime);
        queryWrapper.ge("end_date",dateTime);
        queryWrapper.last("limit 1");

        Quarter quarter = quarterMapper.selectOne(queryWrapper);

        Integer yearAndQuarter = 0;

        if(quarter != null){
            //throw new CommonException(ResultCode.quarterNo);
            yearAndQuarter = Integer.parseInt(quarter.getYearPart()+""+quarter.getNum());
        }


        QueryWrapper<OrderDetail> orderDetailQueryWrapper = new QueryWrapper<>();
        orderDetailQueryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        orderDetailQueryWrapper.eq("deleted",0);
        orderDetailQueryWrapper.ne("status",2);
        orderDetailQueryWrapper.in("order_type",1,3,5);
        if(yearAndQuarter.equals(0)){
            orderDetailQueryWrapper.eq("year_and_quarter",0);
        }else {
            orderDetailQueryWrapper.ge("year_and_quarter",yearAndQuarter);
        }


        List<String> orderBy = new ArrayList<>();
        orderBy.add("id");
        orderBy.add("student_id");
        orderDetailQueryWrapper.orderByAsc(orderBy);

        List<String> groupBy = new ArrayList<>();
        groupBy.add("student_id");

        if(StringUtils.isNotBlank(subjectsId)){
            groupBy.add("subjects_id");
            orderDetailQueryWrapper.eq(StringUtils.isNotBlank(subjectsId),"subjects_id",subjectsId);
        }
        orderDetailQueryWrapper.groupBy(groupBy);

        IPage<StatementData> statementDataIPage =  orderDetailMapper.statementData(page,orderDetailQueryWrapper);

        for (StatementData statementData:statementDataIPage.getRecords()){
            Student student = studentMapper.selectById(statementData.getStudentId());
            if(student != null){
                statementData.setStudentName(student.getName());
            }
            Grade grade = gradeMapper.selectById(statementData.getGradeId());
            if(grade !=null){
                statementData.setGradeId(grade.getId());
                statementData.setYearClassId(grade.getYearClassId().getKey());
                statementData.setYearClassName(grade.getYearClassId().getValue());
            }

            QueryWrapper<OrderApply> orderApplyQueryWrapper = new QueryWrapper<>();
            orderApplyQueryWrapper.eq(StringUtils.isNotBlank(schoolId),"b.school_id",schoolId);
            orderApplyQueryWrapper.eq(StringUtils.isNotBlank(subjectsId),"b.subjects_id",subjectsId);
            if(yearAndQuarter.equals(0)){
                orderApplyQueryWrapper.eq("b.year_and_quarter",0);
            }else {
                orderApplyQueryWrapper.ge("b.year_and_quarter",yearAndQuarter);
            }

            //orderApplyQueryWrapper.eq("b.deleted",0);
            orderApplyQueryWrapper.ne("a.status",2);
            orderApplyQueryWrapper.eq("a.order_type",4);
            orderApplyQueryWrapper.eq("b.student_id",statementData.getStudentId());
            BigDecimal outPrice = orderDetailMapper.getStudentOutPrice(orderApplyQueryWrapper);
            statementData.setOutPrice(outPrice);

            BigDecimal advancePrice = statementData.getSolidPrice().subtract(outPrice).subtract(statementData.getRealityPirce());
            statementData.setAdvancePrice(advancePrice);

            //System.out.println("statementData:"+statementData.toString());
            //System.out.println();
        }

        return statementDataIPage;
    }
}
